<?php

namespace Api\Controller;

use Think\Controller;
use Think\Exception;
use Think\Log;
use Think\Upload\Driver\Qclouds;

class IndexController extends Controller
{
    public function addAddress()
    {

        $preDb = I('pre', '');
        if (empty($preDb)) {
            $this->returnerror('pre', 'ok');
        }
        $userModel = M('wx_user u', $preDb);
//        $count = $userModel->where(array('status' => 1))->count();
        $pagesize = 800;//一页条数
//        $rows = ceil($count / $pagesize);//总页数
        $page = I('page', 0);
        if ($page == 0) {
            $this->returnerror('page', 'ok');
        }
        $firstRows = ($page - 1) * $pagesize;
        $user = $userModel->field('u.id,u.last_login_ip,ua.province,ua.city')
            ->join("left join " . $preDb . "user_address ua on ua.user_id = u.id")
            ->limit($firstRows, $pagesize)
            ->select();
        $addressModel = M('user_address', $preDb);
        foreach ($user as $key => $list) {
            $user_id = $list['id'];
            if (empty($list['province']) && $list['province'] !== 0) {
                $address_detail = get_user_addr($list['last_login_ip'])['content']['address_detail'];
                $province = $address_detail['province'];
                $city = $address_detail['city'];
                if (!empty($province)) {
                    $dataArr = array('user_id' => $user_id, 'province' => $province, 'city' => $city);
                    $res = $addressModel->add($dataArr);
                }
            }
        }
        $this->returnok($page, 'ok');
    }
}


